package com.zonski.jbots.game.entity;

import java.util.Hashtable;

import com.zonski.jbots.engine.Modes;
import com.zonski.jbots.engine.Engine;
import com.zonski.jbots.engine.Entity;
import com.zonski.jbots.engine.EntityFactory;
import com.zonski.jbots.engine.entity.AbstractMind;
import com.zonski.jbots.engine.entity.Mind;

import com.zonski.jbots.game.JBotsConstants;
import com.zonski.jbots.game.EntityHelper;

/**
 * Mind for an egg, the egg will hatch into something after a certain 
 * period of time. Eggs accept a certain amount of charge which determines
 * what is created
 */
public class EggMind extends AbstractMind
{
    private static final int CHARGE_MULT = 5;
    private static final int DEFAULT_SHOW_SHELL_FOR = 0 ;
    private static final int MAX_CHARGE_SIZE = 100 * CHARGE_MULT;
    private static final int MIN_CHARGE_SIZE = 50 * CHARGE_MULT;
    private static final int MAX_HEIGHT = 12;

    private String toCreate;
    private int cx;
    private int cy;
    private int showShellFor = DEFAULT_SHOW_SHELL_FOR;

    public EggMind()
    {
    }

    public String getToCreate()
    {
        return this.toCreate;
    }

    public void setToCreate(String toCreate)
    {
        this.toCreate = toCreate;
    }

    public void update(Engine engine)
        throws Exception
    {
        if(this.getEntity().getMode() == Modes.NONE)
        {
            int updates = this.getEntity().getEffectiveModeUpdates();
            if(updates == 1)
            {
                this.cx = this.getEntity().bounds.getCentreX();
                this.cy = this.getEntity().bounds.getCentreY();
            }
            int chargeTime = this.getEntity().getCharge() * CHARGE_MULT;
            // create the oval
            int b = ((12 * (updates + MIN_CHARGE_SIZE)) / MAX_CHARGE_SIZE);
            if(b > MAX_HEIGHT)
            {
                b = MAX_HEIGHT;
            }
            this.getEntity().bounds.setDimensions((b*3)/4, b);
            if(updates >= chargeTime)
            {
                this.getEntity().bounds.setCentre(this.cx, this.cy);
                hatch(engine);
                this.getEntity().setMode(Modes.DEAD);
            }else if(updates > (chargeTime * 4) / 5){
                if(updates % 4 == 0)
                {
                    // shake the egg
                    int dx;
                    int dy;
                    dx = (JBotsConstants.RANDOM.nextInt() % 2);
                    dy = (JBotsConstants.RANDOM.nextInt() % 2);
                    this.getEntity().bounds.setCentre(this.cx + dx, this.cy + dy);
                }
            }else{
                this.getEntity().bounds.setCentre(this.cx, this.cy);
            }

        }else if(this.getEntity().getMode() == Modes.DEAD){
            int updates = this.getEntity().getEffectiveModeUpdates();
            if(updates > showShellFor)
            {
                this.getEntity().setRemovable(true);
            }
        }
    }

    private void hatch(Engine engine)
        throws Exception
    {
        EntityFactory entityFactory = engine.getEntityFactory();
        Entity entity = entityFactory.createEntity(this.getToCreate());

        entity.setMaxCharge(this.getEntity().getMaxCharge());
        entity.setCharge(this.getEntity().getCharge());

        EntityHelper.centreAtClosestTile(
                entity.bounds, this.getEntity().bounds, engine.getRoom()
        );
        entity.layerId = this.getEntity().layerId;
        engine.addEntity(entity);
    }

    public Mind copy()
    {
        return new EggMind();
    }
}
